﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;



namespace MaintainSchools
{
    public partial class Form1 : Form
    {
        public static ListViewItem item;
        public static School LastClicked;
        public static Random rand;
        public Form1()
        {
            rand = new Random();
            Globals.roles = new Dictionary<int,Role>();
            Globals.roles.Add(1, new Role(1, "Staff"));
            Globals.roles.Add(2, new Role(2, "Coordinator"));
            Globals.roles.Add(3, new Role(3, "Teacher"));

            Globals.users = new Dictionary<int, User>();
            int r = rand.Next(0,9999);
            Globals.users.Add(r, new User(r, "TBarkly", "1234", 2));
            int r1 = rand.Next(0, 9999);
            Globals.users.Add(r1, new User(r1, "BSmith", "2345", 2));
            int r2 = rand.Next(0, 9999);
            Globals.users.Add(r2, new User(r2, "BBarker", "3456", 2));

            Globals.schools = new Dictionary<int, School>();
            int r3 = rand.Next(0, 9999);
            School t = new School(r3, "Greenville High School", Globals.users[r].userID);
            Globals.schools.Add(r3, t);
            r3 = rand.Next(0, 9999);
            t = new School(r3, "East Side High School", Globals.users[r1].userID);
            Globals.schools.Add(r3, t);
            r3 = rand.Next(0, 9999);
            t = new School(r3, "West Side High School", Globals.users[r2].userID);
            Globals.schools.Add(r3, t);

            InitializeComponent();
            SchoolView.ItemActivate +=new EventHandler(AddressView_ItemActivate);
            LastClicked = new School(0,"",0);
            item = new ListViewItem();
            foreach (int i in Globals.schools.Keys)
            {
                string[] temp = new string[] {
                    Globals.schools[i].schoolID.ToString(),
                    Globals.schools[i].schoolName,
                    Globals.users[Globals.schools[i].userID].userName
                };
                SchoolView.Items.Add(new ListViewItem(temp));
            }
            
        }
        private void SubmitBtn_Click(object sender, EventArgs e)
        {

            if (LastClicked.schoolID == 0)
            {
                //create
                LastClicked.schoolID = new Random().Next(0, 1000000);
                LastClicked.schoolName = Nametxt.Text;
                if (LastClicked.schoolName == "")
                {
                    MessageBox.Show("The name of a school cannnot be blank!", "Required Data is missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    string[] temp = new string[] {
                        LastClicked.schoolID.ToString(),
                        LastClicked.schoolName,
                        Globals.users[LastClicked.userID].userName
                    };
                    SchoolView.Items.Add(new ListViewItem(temp));
                    Nametxt.Text = "";
                }
            }
            else
            {
                SchoolView.Items.Remove(item);
                string[] temp = new string[] {
                        LastClicked.schoolID.ToString(),
                        LastClicked.schoolName,
                        Globals.users[LastClicked.userID].userName
                    };
                SchoolView.Items.Add(new ListViewItem(temp));
                //update
                Nametxt.Text = "";
            }
            
            
        }
        public void AddressView_ItemActivate(Object sender, EventArgs e)
        {
            item = SchoolView.SelectedItems[0];
            LastClicked.schoolID = Convert.ToInt32(item.SubItems[0].Text);
            LastClicked.schoolName = item.SubItems[1].Text;
            foreach (int i in Globals.users.Keys)
            {
                if (Globals.users[i].userName == item.SubItems[2].Text)
                {
                    LastClicked.userID = i;
                }
            }
            Nametxt.Text = LastClicked.schoolName;
            DeleteBtn.Enabled = true;
            
        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Really delete?", "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SchoolView.Items.Remove(item);
            }
            Nametxt.Text = "";
        }

        private void CreateBtn_Click(object sender, EventArgs e)
        {
            Nametxt.Text = "";
            LastClicked.schoolID = 0;
            LastClicked.schoolName = "";
            LastClicked.userID = 0;
        }

        private void SchoolView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void AddCoord_Click(object sender, EventArgs e)
        {
            int coorID = rand.Next(0, 10000);
            if (LastClicked.userID == 0)
            {
                LastClicked.userID = coorID;
                Globals.users[coorID] = new User(coorID, "", "", 2);
            }
            else
            {
                coorID = LastClicked.userID;
            }
            new CoordinatorInfo(coorID).ShowDialog();
        }
    }
}
